package com.ruoyi.project.system.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.project.system.domain.TRecruitPersonResume;
import com.ruoyi.project.system.domain.vo.RecruitPersonVo;
import com.ruoyi.project.system.service.ITRecruitPersonResumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TRecruitPerson;
import com.ruoyi.project.system.service.ITRecruitPersonService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.transform.Result;

/**
 * 义工信息Controller
 *
 * @author tianchao
 * @date 2021-09-05
 */
@RestController
@RequestMapping("/person/person")
@Slf4j
public class TRecruitPersonController extends BaseController {
    @Autowired
    private ITRecruitPersonService tRecruitPersonService;

    @Autowired
    private ITRecruitPersonResumeService itRecruitPersonResumeService;


    @PostMapping("import/excel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {

        if (file.isEmpty()) {
            return AjaxResult.error("上传失败");
        }
        Map<String, Object> result = tRecruitPersonService.parseExcel(file);

        Integer status = (Integer) result.get("status");
        String msg = result.get("msg").toString();
        return status == 200 ? AjaxResult.success(msg) : AjaxResult.error(msg);

    }

    /**
     * 查询义工信息列表
     */
    @PreAuthorize("@ss.hasPermi('person:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(TRecruitPerson tRecruitPerson) {
        startPage();
        List<TRecruitPerson> list = tRecruitPersonService.selectTRecruitPersonList(tRecruitPerson);
        return getDataTable(list);
    }

    /**
     * 查询义工信息列表
     */
    @GetMapping("/listForSelect")
    public AjaxResult listForSelect(TRecruitPerson tRecruitPerson) {
        List<TRecruitPerson> list = tRecruitPersonService.listForSelect(tRecruitPerson);
        return AjaxResult.success(list);
    }

    /**
     * 导出义工信息列表
     */
    @PreAuthorize("@ss.hasPermi('person:person:export')")
    @Log(title = "义工信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRecruitPerson tRecruitPerson) {
        List<TRecruitPerson> list = tRecruitPersonService.selectTRecruitPersonList(tRecruitPerson);
        ExcelUtil<TRecruitPerson> util = new ExcelUtil<TRecruitPerson>(TRecruitPerson.class);
        return util.exportExcel(list, "义工信息数据");
    }

    /**
     * 获取义工信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('person:person:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tRecruitPersonService.selectTRecruitPersonById(id));
    }

    /**
     * 新增义工信息
     */
    @PreAuthorize("@ss.hasPermi('person:person:add')")
    @Log(title = "义工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecruitPersonVo tRecruitPerson) {
        String personId = IdUtils.fastSimpleUUID();
        tRecruitPerson.setId(personId);
        tRecruitPersonService.insertTRecruitPerson(tRecruitPerson);
        saveResume(tRecruitPerson, personId);
        //初始化见习表 TODO
        return toAjax(true);
    }

    /**
     * 保存简历信息
     *
     * @param tRecruitPerson
     * @param personId
     */
    private void saveResume(@RequestBody RecruitPersonVo tRecruitPerson, String personId) {
        List<TRecruitPersonResume> resumeList = tRecruitPerson.getResumeList();
        //保存简历表
        resumeList.forEach(i -> {
            TRecruitPersonResume item = new TRecruitPersonResume();
            item.setPersonId(personId);
            item.setContent(i.getContent());
            item.setEndDate(i.getEndDate());
            item.setStartDate(i.getStartDate());
            item.setPlace(i.getPlace());
            item.setCreateTime(new Date());
            item.setCreateBy(getUsername());
            itRecruitPersonResumeService.save(item);
        });
    }

    /**
     * 修改义工信息
     */
    @PreAuthorize("@ss.hasPermi('person:person:edit')")
    @Log(title = "义工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecruitPersonVo tRecruitPerson) {
        //删除旧简历表，然后重新提交
        String personId = tRecruitPerson.getId();
        itRecruitPersonResumeService.remove(new LambdaQueryWrapper<TRecruitPersonResume>().eq(TRecruitPersonResume::getPersonId, personId));
        saveResume(tRecruitPerson, personId);
        return toAjax(tRecruitPersonService.updateTRecruitPerson(tRecruitPerson));
    }

    /**
     * 删除义工信息
     */
    @PreAuthorize("@ss.hasPermi('person:person:remove')")
    @Log(title = "义工信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tRecruitPersonService.deleteTRecruitPersonByIds(ids));
    }

}
